/**
 * @file setting_flying_mode.c
 *
 */

/*********************
 *      INCLUDES
 *********************/
#include "lv_watch.h"

#if USE_LV_WATCH_FLYING_MODE != 0

/*********************
 *      DEFINES
 *********************/

/**********************
 *      TYPEDEFS
 **********************/


/**********************
 *  STATIC PROTOTYPES
 **********************/
static lv_obj_t * flying_mode_create(lv_obj_t * activity_obj);

/**********************
 *  STATIC VARIABLES
 **********************/
static bool g_flying_mode = false;

/**********************
 *      MACROS
 **********************/

/**********************
 *   GLOBAL FUNCTIONS
***********************/
void flying_mode_init(uint8_t flying_mode_switch)
{
    g_flying_mode = (1 == flying_mode_switch) ? true : false;
}

void flying_mode_create_btn_action(lv_obj_t * btn, lv_event_t event)
{
    (void)btn;

    if(LV_EVENT_CLICKED != event) return;
    lv_watch_png_cache_all_free();

    lv_obj_t * obj = flying_mode_create(NULL);
    LV_ASSERT_MEM(obj);
}

/**********************
 *   STATIC FUNCTIONS
 **********************/
static lv_flying_mode_obj_ext_t * flying_mode_get_ext(void)
{
    lv_flying_mode_obj_ext_t  * ext = NULL;
    lv_obj_t * watch_obj = NULL;

    lv_obj_t * activity_obj = lv_watch_get_activity_obj(ACT_ID_FLYING_MODE);
    if(activity_obj) {
        lv_watch_get_child_obj(activity_obj, lv_watch_obj_signal, &watch_obj);
    }
    if(watch_obj) {
        ext = lv_obj_get_ext_attr(watch_obj);
    }
    return ext;
}

static void flying_mode_write_nv(void)
{
    uint8_t data = (g_flying_mode) ? 1 : 0;
    UI_NV_Write_Req(NV_SECTION_UI_SETTINGS, NV_APN_OFFSET + sizeof(nv_apn_info) * 2 + 2, 1, &data);
}

static void flying_mode_on_btn_action(lv_obj_t * btn, lv_event_t event)
{
    if(LV_EVENT_CLICKED != event) return;

    lv_flying_mode_obj_ext_t * ext = flying_mode_get_ext();
    if(0 == ext->lv_watch_obj.element_click_en) return;

    printf("%s: flying mode: %d\n", __FUNCTION__, g_flying_mode);

    lv_checkbox_set_checked(ext->cb_on, true);
    if(!g_flying_mode) {
        lv_checkbox_set_checked(ext->cb_off, false);
        watch_modem_power_off_req();
        g_flying_mode = true;
        if(watch_modem_sim_present_check_req()) {
            lv_obj_t * list_btn = setting_get_list_btn(WATCH_TEXT_ID_MOBILE_NETWORK);
            if(list_btn) {
                lv_obj_set_click(list_btn,false);
                lv_obj_set_style_local_text_color(lv_obj_get_child(list_btn, NULL), LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_GRAY);
            }
        }
        flying_mode_write_nv();
    }
}

static void flying_mode_off_btn_action(lv_obj_t * btn, lv_event_t event)
{
    if(LV_EVENT_CLICKED != event) return;

    lv_flying_mode_obj_ext_t * ext = flying_mode_get_ext();
    if(0 == ext->lv_watch_obj.element_click_en) return;

    printf("%s: flying mode: %d\n", __FUNCTION__, g_flying_mode);

    lv_checkbox_set_checked(ext->cb_off, true);
    if(g_flying_mode) {
        lv_checkbox_set_checked(ext->cb_on, false);
        watch_modem_power_up_req();
        g_flying_mode = false;
        if(watch_modem_sim_present_check_req()) {
            lv_obj_t * list_btn = setting_get_list_btn(WATCH_TEXT_ID_MOBILE_NETWORK);
            if(list_btn){
                lv_obj_set_click(list_btn,true);
                lv_obj_set_style_local_text_color(lv_obj_get_child(list_btn, NULL), LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_WHITE);
            }
        }
        flying_mode_write_nv();
    }
}

static lv_obj_t * flying_mode_create(lv_obj_t * activity_obj)
{
    if(activity_obj == NULL) {
        lv_watch_activity_ext_t activity_ext;
        memset(&activity_ext, 0, sizeof(lv_watch_activity_ext_t));
        activity_ext.actId = ACT_ID_FLYING_MODE;
        activity_ext.create = flying_mode_create;
        activity_ext.prepare_destory = NULL;
        activity_obj = lv_watch_creat_activity_obj(&activity_ext);
        LV_ASSERT_MEM(activity_obj);
        if(activity_obj == NULL) return NULL;
    }

    lv_obj_t * obj = lv_watch_obj_create(activity_obj);
    LV_ASSERT_MEM(obj);
    if(obj == NULL) return NULL;
    lv_flying_mode_obj_ext_t * ext = lv_obj_allocate_ext_attr(obj, sizeof(lv_flying_mode_obj_ext_t));
    LV_ASSERT_MEM(ext);

    lv_obj_t * label = lv_label_create(obj, NULL);
    lv_obj_add_style(label, LV_LABEL_PART_MAIN, &lv_watch_font20_gray);
    lv_label_set_align(label, LV_LABEL_ALIGN_CENTER);
    lv_obj_set_click(label, false);
    lv_label_set_text_id(label, WATCH_TEXT_ID_FLYING_MODE);
    lv_obj_align(label, obj, LV_ALIGN_IN_TOP_MID, 0, 10);

    lv_obj_t * cont = lv_cont_create(obj, NULL);
    lv_obj_add_style(cont, LV_OBJ_PART_MAIN, &lv_style_transp_fit);
    if(lv_obj_get_base_dir(obj) == LV_BIDI_DIR_RTL)
        lv_btn_set_layout(cont, LV_LAYOUT_COLUMN_RIGHT);
    else
        lv_btn_set_layout(cont, LV_LAYOUT_COLUMN_LEFT);
    lv_obj_align(cont, NULL, LV_ALIGN_CENTER, -10, -20);
    lv_cont_set_fit2(cont, LV_FIT_TIGHT, LV_FIT_TIGHT);
    lv_obj_set_click(cont, false);

    lv_obj_t * cb_on = lv_checkbox_create(cont, NULL);
    lv_checkbox_set_text(cb_on, lv_lang_get_text(WATCH_TEXT_ID_ON));
    lv_obj_clean_style_list(cb_on, LV_CHECKBOX_PART_BULLET);
    lv_obj_add_style(cb_on, LV_CHECKBOX_PART_BULLET, &lv_watch_cb_styles);
    lv_obj_set_event_cb(cb_on, flying_mode_on_btn_action);
    lv_watch_obj_add_element(cb_on);

    lv_obj_t * cb_off = lv_checkbox_create(cont, cb_on);
    lv_checkbox_set_text(cb_off, lv_lang_get_text(WATCH_TEXT_ID_OFF));
    lv_obj_set_event_cb(cb_off, flying_mode_off_btn_action);
    lv_watch_obj_add_element(cb_off);

    printf("%s: flying_mode %d\n", __FUNCTION__, g_flying_mode);
    if(g_flying_mode) {
        lv_checkbox_set_checked(cb_on, true);
    } else {
       lv_checkbox_set_checked(cb_off, true);
    }

    ext->cb_on = cb_on;
    ext->cb_off = cb_off;
    return obj;
}
#endif /*USE_LV_WATCH_FLYING_MODE*/

bool flying_mode_get_state(void)
{
#if USE_LV_WATCH_FLYING_MODE != 0
    return g_flying_mode;
#else
    return false;
#endif /*USE_LV_WATCH_FLYING_MODE*/
}
